/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package presenter;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import view.TelaCodigo;
import view.TelaGrafica;
import view.TelaView;

/**
 *
 * @author Devair
 */
public class TelaActionListeners {

    static class NovoActionListener implements ActionListener {

        TelaPresenter presenter;

        public NovoActionListener(TelaPresenter presenter) {
            this.presenter = presenter;
        }

        @Override
        public void actionPerformed(ActionEvent e) {
            TelaView view = (TelaGrafica) presenter.getView();
            presenter.novoArquivo();
            presenter.novoArqCod();
        }
    }

    static class AbrirActionListener implements ActionListener {

        TelaPresenter presenter;

        public AbrirActionListener(TelaPresenter presenter) {
            this.presenter = presenter;
        }

        @Override
        public void actionPerformed(ActionEvent e) {

            try {
                                
                presenter.abrirArquivo(this.presenter.getView().getArquivo());

            } catch (FileNotFoundException ex) {
                Logger.getLogger(TelaActionListeners.class.getName()).log(Level.SEVERE, null, ex);
            } catch (IOException ex) {
                Logger.getLogger(TelaActionListeners.class.getName()).log(Level.SEVERE, null, ex);
            }

        }
    }

    static class GravarActionListener implements ActionListener {

        TelaPresenter presenter;

        public GravarActionListener(TelaPresenter presenter) {
            this.presenter = presenter;
        }

        @Override
        public void actionPerformed(ActionEvent e) {
            TelaView view = (TelaGrafica) presenter.getView();

            try {

                File arquivo = view.getArquivo();
                presenter.gravarArquivo(arquivo, false);

            } catch (FileNotFoundException ex) {
                Logger.getLogger(TelaActionListeners.class.getName()).log(Level.SEVERE, null, ex);
            } catch (IOException ex) {
                Logger.getLogger(TelaActionListeners.class.getName()).log(Level.SEVERE, null, ex);
            }

        }
    }

    static class GravarComoActionListener implements ActionListener {

        TelaPresenter presenter;

        public GravarComoActionListener(TelaPresenter presenter) {
            this.presenter = presenter;
        }

        @Override
        public void actionPerformed(ActionEvent e) {
            TelaView view = (TelaGrafica) presenter.getView();

            try {

                File arquivo = view.getArquivo();
                presenter.gravarArquivo(arquivo, true);

            } catch (FileNotFoundException ex) {
                Logger.getLogger(TelaActionListeners.class.getName()).log(Level.SEVERE, null, ex);
            } catch (IOException ex) {
                Logger.getLogger(TelaActionListeners.class.getName()).log(Level.SEVERE, null, ex);
            }

        }
    }

    static class VerificarActionListener implements ActionListener {

        TelaPresenter presenter;

        public VerificarActionListener(TelaPresenter presenter) {
            this.presenter = presenter;
        }

        @Override
        public void actionPerformed(ActionEvent e) {

            presenter.analiseLexica();

            presenter.analiseSintatica();

        }
    }

    
    static class TabelaSimboloListener implements ActionListener{

        TelaPresenter presenter;
        
        public TabelaSimboloListener(TelaPresenter presenter) {
            this.presenter = presenter;
        }       
        
        @Override
        public void actionPerformed(ActionEvent e) {
            
            this.presenter.mostraTabelaSimbolos();
            
        }                        
    }
    
     static class TelaCodigoListener implements ActionListener{

        TelaPresenter presenter;
        
        public TelaCodigoListener(TelaPresenter presenter) {
            this.presenter = presenter;
        }       
        
        @Override
        public void actionPerformed(ActionEvent e) {
            
            this.presenter.mostraTelaCodigo();
            
        }                        
    }
    
    static class GravarCodigoActionListener implements ActionListener {

        TelaPresenter presenter;

        public GravarCodigoActionListener(TelaPresenter presenter) {
            this.presenter = presenter;
        }

        @Override
        public void actionPerformed(ActionEvent e) {
            TelaCodigo view = presenter.getViewCodigo();

            try {
                
                presenter.novoArqCod();
                
                File arquivo = view.getArquivo();
                presenter.gravarArquivoCod(arquivo, true);
                

            } catch (FileNotFoundException ex) {
                Logger.getLogger(TelaActionListeners.class.getName()).log(Level.SEVERE, null, ex);
            } catch (IOException ex) {
                Logger.getLogger(TelaActionListeners.class.getName()).log(Level.SEVERE, null, ex);
            }

        }
    } 
     
    static class SairActionListener implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent e) {
            System.exit(0);
        }
    }
}
